import { Button, Divider, Dropdown, Menu, message } from 'antd'
import { PlusOutlined } from '@ant-design/icons'
import React, { useState, useRef, useEffect } from 'react'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table'
import { connect, history, Link } from 'umi'
import { queryRule, addInfo, saveArchiveRemark } from '../service'
import { yesOrNoList, socialTraineeStateArray, employeeStateList, isOfferList ,isSubmitList, isHireList} from '@/utils/constant'
import CreateForm from './components/CreateForm'
import Termination from './components/Termination'

const TableList = (props) => {
  const actionRef = useRef()
  const [done, setDone] = useState(false)
  const [visible, setVisible] = useState(false)
  const [current, setCurrent] = useState(undefined)
  const [oid,setOid] = useState()
  const [terminationVisible,setTerminationVisible] = useState(false)


  const itemEdit = (item) => {
    history.push(`/info/edit/${item.oid}`)
  }

  const itemDetail = (item) => {
    history.push(`/info/details/${item.oid}`)
  }

  const termination = (e,record) => {
    setTerminationVisible(true)
    setOid(record.oid)
  }

  const columns = [
    {
      title: '编号',
      dataIndex: 'employeeOid',
      hideInSearch: true,
    },
    {
      title: '姓名',
      dataIndex: 'name',
      render: (item, record) => (
        <Link to={`/info/details/${record.oid}`}>{item}</Link>
      ),
    },
    {
      title: '公司',
      dataIndex: 'orgname',
    },
    {
      title: '部门',
      dataIndex: 'unitname',
    },
    {
      title: '岗位',
      dataIndex: 'jobname',
    },
    {
      title: '职级',
      dataIndex: 'jobLevel',
      hideInSearch: true,
    },
    {
      title: '录用状态',
      dataIndex: 'status',
      hideInSearch: true,
      valueEnum: {
        0: { text: '待发起', status: 'Default' },
        1: { text: '填写完已保存', status: 'Default' },
        2: { text: '审批中', status: 'Processing' },
        3: { text: '已审批', status: 'Success' },
        4: { text: '作废', status: 'Default' },
        5: { text: 'Offer已发送', status: 'Success' },
        6: { text: '驳回', status: 'Default' },
      },
    },
    {
      title: 'Offer发送',
      dataIndex: 'isOffer',
      hideInSearch: true,
      valueEnum: {
        0: { text: isOfferList[0].label, status: 'Default' },
        1: { text: isOfferList[1].label, status: 'Success' },
      },
    },
    {
      title: '信息采集',
      dataIndex: 'isMemberDone',
      hideInSearch: true,
      valueEnum: {
        0: { text: isSubmitList[0].label, status: 'Default' },
        1: { text: isSubmitList[1].label, status: 'Success' },
      },
    },
    {
      title: '是否入职',
      dataIndex: 'isHireDone',
      hideInSearch: true,
      valueEnum: {
        0: { text: isHireList[0].label, status: 'Default' },
        1: { text: isHireList[1].label, status: 'Success' },
      },
    },
    {
      title: '招聘类型',
      dataIndex: 'socialTrainee',
      valueEnum: {
        0: { text: socialTraineeStateArray[0] },
        1: { text: socialTraineeStateArray[1] },
      },
    },
    {
      title: '创建人',
      dataIndex: 'principal',
      hideInSearch: true,
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (item, record) => (
        <>
          <a onClick={() => itemEdit(record)}>修改</a>
          <Divider type="vertical"/>
          <a onClick={() => itemDetail(record)}>详情</a>
          {
            !(record.status === 2 || record.isHireDone === 1) && <>
              <Divider type="vertical"/>
              <a onClick={(e) => termination(e,record)}>终止</a>
            </>
          }
        </>
      ),
    },
  ]

  const handleModalVisible = (value) => {
    setVisible(value)
  };

  const handleCancel = () => {
    setVisible(false);
    setTerminationVisible(false);
  };

  const handleSubmit = (values) => {
    return addInfo(values).then((res)=>{
      if(res.flag){
        setDone(true);
        setVisible(false);
        message.success('新建成功！');
        actionRef.current.reload();
      }else {
        message.error(res.error);
      }
    })
  };

  const handleSaveTermination = (value) => {
    setTerminationVisible(false)
    saveArchiveRemark(value).then(res => {
      if (res.flag){
        message.success(`终止成功！`);
        actionRef.current.reload();
      } else {
        message.error(res.error);
      }
    })
  };

  return (
    <PageHeaderWrapper>
      <ProTable
        actionRef={actionRef}
        rowKey="oid"
        request={(params) => queryRule(params)}
        columns={columns}
        toolBarRender={(action, { selectedRows }) => [
          <Button type="primary" onClick={() => handleModalVisible(true)}>
            <PlusOutlined/> 新建
          </Button>,
        ]}
      />
      <CreateForm
        values={{}}
        done={done}
        current={current}
        visible={visible}
        onCancel={handleCancel}
        onSubmit={handleSubmit}
      />
      <Termination
        visible={terminationVisible}
        oid={oid}
        onCancel={handleCancel}
        onSubmit={handleSaveTermination}
      />
    </PageHeaderWrapper>
  )
}

export default connect(({ productAndCategory, loading }) => ({
  productAndCategory,
  loading: loading.models.productAndCategory,
}))(TableList)
